﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Reflection;
using PBDesk.Business.FeedsAggregator.Entities;
using PBDesk.Utilities;


namespace PBDesk.Business.FeedsAggregator.Helper
{
    public class CategoryHelper
    {

        
        public static FeedCategory GetFeedCategory(string categoryName)
        {
            FeedCategory retVal = null;
            if (categoryName.IsNotNullAndNotEmpty())
            {
                categoryName = categoryName.ToLower();

                try
                {
                    XDocument xDoc = SyndicationHelper.GetXMLResource();
                    if (xDoc != null)
                    {
                        var list = from category in xDoc.Descendants("Category")
                                   where category.Attribute("name").Value == categoryName && category.Attribute("IsActive").Value == "Y"
                                   select new FeedCategory
                                   {
                                       Name = category.Attribute("name").Value,
                                       Description = category.Descendants("description").FirstOrDefault().Value,
                                       Title = category.Descendants("title").FirstOrDefault().Value,
                                       ImageUrl = new Uri(category.Descendants("imageUrl").FirstOrDefault().Value),
                                       Link = new Uri(category.Descendants("link").FirstOrDefault().Value),
                                       FeedItems = (from feed in category.Descendants("Feed")
                                                    where feed.Attribute("IsActive").Value == "Y"
                                                    select new FeedItem
                                                    {
                                                        Name = feed.Attribute("name").Value,
                                                        Description = feed.Descendants("description").FirstOrDefault().Value,
                                                        Url = feed.Descendants("url").FirstOrDefault().Value
                                                    }).ToList<FeedItem>()
                                   };

                        if (list != null && list.FirstOrDefault() != null)
                            retVal = list.FirstOrDefault();
                    }
                    else
                    {
                        throw new Exception("XDocument xDoc is null for categoryName=" + categoryName);
                    }
                }
                catch (Exception ex)
                {
                    
                }
            }
            if (retVal == null)
                retVal = GetFeedCategory("feed404");

            return retVal;
        }

      
        
        public static FeedCategory GetFeedCategory(string categoryName, string subCategoryName)
        {
            return GetFeedCategory(categoryName + "-" + subCategoryName);
        }



       
    }
}
